﻿using System;
using System.Diagnostics;
using System.Windows.Forms;
using ClassLibrary1;
using DPLAB.IPAMS.DataEntities;
using DPLAB.IPAMS.DataLoader.Excel;

namespace DPLAB.IPAMS.IPAMSGui.Form
{
    public partial class frmMain : frmBase
    {
        #region PrivateMember

        private static frmMain m_theInstance;

        #endregion

        #region Public Access 

        public IPAMSDataSet IpamsDataSet { get; set; }

        #endregion

        public frmMain()
        {
            InitializeComponent();
        }

        public static frmMain GetInstance()
        {
            if (null == m_theInstance)
            {
                // no previous instance, so create an instance of this class
                m_theInstance = new frmMain();
            }

            // return the instance of the class
            return m_theInstance;
        }

        private void exitApp()
        {
            // if we should close the form - then close it...
            if (m_theInstance != null)
                Close();

            // exit from the application
            Application.DoEvents();
            Application.Exit();

            // kill the current process
            Process.GetCurrentProcess().Kill();
        }

        private void formClosed()
        {
            // the form was closed, so set the current instance to null (by doing so,
            //      we will make the grabage collector - to dispose the old instance)
            m_theInstance = null;

            // exit from the application
            exitApp();
        }

        private void mnuProcessOne_Click(object sender, EventArgs e)
        {
            frmWait.ShowWaitScreen("Please Wait While Proccess take Place ....");
            var kernel = new IPAMSKernel(IpamsDataSet);
            kernel.QuantityTransactions2Transactions();

            var displayForm = new frmDisplayData(IpamsDataSet);
            frmWait.CloseWaitScreen();

            displayForm.ShowDialog();
        }

        #region Mnu & btn Event Code 

        private void mnuLoadExcel_Click(object sender, EventArgs e)
        {
            var frm = new frmLoadExcelFile();
            frm.ShowDialog();

            if (frm.FileName == "")
                return;

            frmWait.ShowWaitScreen("Please Wait While Proccess take Place ....");

            var loader = new ExcelLoader(frm.FileName);

//            IpamsDataSet = new DPLAB.IPAMS.DataEntities.IPAMSDataSet(loader.Load);

            //IpamsDataSet = (DPLAB.IPAMS.DataEntities.IPAMSDataSet)(loader.Load().Copy());

            IpamsDataSet = loader.Load();

            var displatForm = new frmDisplayData(IpamsDataSet);
            frmWait.CloseWaitScreen();

            displatForm.ShowDialog();
        }

        private void mnuAbout_Click(object sender, EventArgs e)
        {
            var frm = new frmAbout();

            frm.ShowDialog();
        }

        private void mnuProcessTwo_Click(object sender, EventArgs e)
        {
            frmWait.ShowWaitScreen("Please Wait While Proccess take Place ....");
            var kernel = new IPAMSKernel(IpamsDataSet);

            kernel.Transactions2QuantityTransactions();

            var displayForm = new frmDisplayData(IpamsDataSet);
            frmWait.CloseWaitScreen();
            displayForm.ShowDialog();
        }

        #endregion
    }
}